<?php
class login_controller extends common {
	// 登录图形验证码
	function captcha() {
		require_once CLASS_PATH . 'verify.class.php';
		verify::entry();
	}
	
	// 登录系统
	function login() {
		require_once CLASS_PATH . 'verify.class.php';
		extract($_REQUEST);
		if (empty($user))
			return msg(1, '请输入用户名！');
		else if (empty($pass))
			return msg(2, '请输入密码！');
		else if (empty($captcha))
			return msg(3, '请输入图形验证码！');
		else if (true !== $ret = verify::check($captcha))
			return msg(4, $ret);
		
		$admin = $this->sql->where("(`user` = '{$user}' OR `mobile` = '{$user}' OR `email` = '{$user}') AND `pass` = '{$pass}'")->select('admin_user', false);
		if (!$admin)
			return msg(5, '用户名或密码错误！');
		
		if ($admin['status'] === 0)
			return msg(6, '此账号暂未审核或已禁用！');
		
		addCookie('token', authcode::encode(json(array(
			'id' => $admin['id'],
			'user' => $admin['user'],
			'pass' => $admin['pass']
		), false), LOGIN_KEY, LOGIN_EXPIRE), 86400);
		
		$this->admin = $admin;
		$this->log('登录系统');
		$this->sql->where("`id` = {$admin['id']}")->update('admin_user', array(
			'loginTime' => date('Y-m-d H:i:s')
		));
		
		return $this->gotoHome(0, '登陆成功！');
	}
}